import React from 'react'
import Loadable from 'react-loadable';
import Loading from './Loading';
import { registerModel } from "./register";

const getRouterData = app => {
  const routes = [
    {
      path: '/',
      exact: true,
      component: (app) => {
        return Loadable.Map({
          loader: {
            IndexPage: () => import('./routes/IndexPage.js'),
          },
          delay: 200,
          timeout: 1000,
          loading: Loading,
          render(loaded, props) {
            let C = loaded["IndexPage"].default;

            return <C {...props} />;
          },
        });
      },
    },
    {
      path: '/home',
      exact: true,
      component: (app) => {
        return Loadable.Map({
          loader: {
            Home: () => import('./routes/Home.js'),
            user: () => import('./models/example.js'),
            grid: () => import('./models/grid.js'),
          },
          delay: 200,
          timeout: 1000,
          loading: Loading,
          render(loaded, props) {
            let C = loaded["Home"].default;
            registerModel(app, user);
            registerModel(app, grid);
            return <C {...props} />;
          },
        });
      },
    }
  ]

  const routerData = routes.map((route) => {
    let componentName = route.component.name;
    if (componentName !== 'Connect') {
      try {
        route.component = route.component(app);
      } catch (e) {
      }
    }
    return route;
  });
  return routerData;
}
export default getRouterData;